package com.yidu.ruifeng_logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.ruifeng_logistics.entity.LogisticsArea;
import com.yidu.ruifeng_logistics.entity.LogisticsOrder;
import com.yidu.ruifeng_logistics.entity.LogisticsOrderVo;
import com.yidu.ruifeng_logistics.service.LogisticsAreaService;
import com.yidu.ruifeng_logistics.service.LogisticsOrderService;
import com.yidu.ruifeng_logistics.utils.BaseMessage;
import com.yidu.ruifeng_logistics.utils.IdWorker;
import com.yidu.ruifeng_logistics.utils.Tools;
import com.yidu.ruifeng_logistics.vo.LogisticsPredictionVo;
import com.yidu.ruifeng_logistics.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (LogisticsOrder)表控制层
 *
 * @author huangwu
 * @since 2021-03-09 14:24:41
 */
@Api(tags = "订单接口")
@RestController
@RequestMapping("logisticsOrder")
public class LogisticsOrderController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsOrderService logisticsOrderService;
    /*
地区
 */
    @Resource
    private LogisticsAreaService logisticsAreaService;


    /**
     * 查询订单
     * @param logisticsOrderVo
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询订单", notes = "返回订单集合")
    @GetMapping("select")
    public Map<String, Object> select(LogisticsOrderVo logisticsOrderVo, Integer page,  Integer limit) {
        //将参数传入分页助手 PageHelper.startPage(page,limit);
        PageHelper.startPage(page,limit);

        List<LogisticsOrderVo> list = logisticsOrderService.queryAll(logisticsOrderVo);
        for (LogisticsOrderVo vo :list){
            //根据出发地ID查询地名
            List<LogisticsArea> area = logisticsAreaService.selectYi(Integer.valueOf(vo.getOrderDeparture()));
            for(LogisticsArea area1:area){
                vo.setOrderDeparture(area1.getAreaName());
            }
            //根据目的地ID查地名
            List<LogisticsArea> areas = logisticsAreaService.selectYi(Integer.valueOf(vo.getOrderBourn()));
            for(LogisticsArea area1:areas){
                vo.setOrderBourn(area1.getAreaName());
            }
        }

        //查询到集合后，将集合数据封装到分页助手的PageInfo对象中
        PageInfo<LogisticsOrderVo> pageInfo=new PageInfo<>(list);
        //返回Tools的Layuimap集合
        return Tools.getLayUiMap(list,(int)pageInfo.getTotal());
    }

    /**
     * 根据客户id查客户所有订单
     * @param logisticsOrderVo
     * @param clientId
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询订单", notes = "返回订单集合")
    @GetMapping("select2")
    public Map<String, Object> selectId(LogisticsOrderVo logisticsOrderVo,Integer clientId, Integer page,  Integer limit) {
        //将参数传入分页助手
        PageHelper.startPage(page,limit);
        //直接用传入的id写进vo查询所有订单
        logisticsOrderVo.setClientId(clientId);

        List<LogisticsOrderVo> list = logisticsOrderService.notQueryAll(logisticsOrderVo);
        //查询到集合后，将集合数据封装到分页助手的PageInfo对象中
        PageInfo<LogisticsOrderVo> pageInfo=new PageInfo<>(list);
        return Tools.getLayUiMap(list,(int)pageInfo.getTotal());
    }

    /**
     * 增加或根据主键修改订单
     *
     * @param orderVo
     * @return
     */
    @ApiOperation(value = "增加或者修改订单", notes = "返回状态")
    @PostMapping("addOrUpdata")
    public BaseMessage addOrUpdata(OrderVo  orderVo) {
        //使用编号生成方法
        IdWorker idWorker = new IdWorker();

        LogisticsOrder logisticsOrder=new LogisticsOrder();
        //BeanUtils.copyProperties（）复制对象
        BeanUtils.copyProperties(orderVo,logisticsOrder);

        BaseMessage baseMessage = new BaseMessage();
        //判断传入对象ID是否为空
        if (logisticsOrder.getOrderId() == null) {
            //ID空则增加订单，生成新的订单编号
            logisticsOrder.setOrderNumber(String.valueOf(idWorker.nextId()));
//            //订单生成时间
//           logisticsOrder.setOrderTime(new Date());
            logisticsOrder.setOrderTime(new Date());
            //新增订单
            logisticsOrderService.insert(logisticsOrder);
            baseMessage.setState(1);
            baseMessage.setMsg("增加成功");
        } else {
            //ID不为空则修改订单
            logisticsOrderService.update(logisticsOrder);
            baseMessage.setState(1);
            baseMessage.setMsg("修改成功");
        }
        return baseMessage;
    }

    /**
     * 根据主键删除订单
     * @param id
     * @return
     */
    @ApiOperation(value = "删除订单",notes = "返回状态")
    @PostMapping("delete")
    public BaseMessage delete(Integer id){
        BaseMessage baseMessage = new BaseMessage();
        //通过ID删除订单
        logisticsOrderService.deleteById(id);
        baseMessage.setState(1);
        baseMessage.setMsg("删除成功");
        return baseMessage;
    }

    /**
     * 根据id 查询关联vo
     * @param orderId
     * @return
     */
    @ApiOperation(value = "根据id查",notes = "返回状态")
    @PostMapping("selectID")
    public BaseMessage selectID(Integer orderId){
        BaseMessage baseMessage = new BaseMessage();
        LogisticsOrderVo vo= logisticsOrderService.selectById(orderId);
        baseMessage.setState(1);
        baseMessage.setMsg("查询成功");
        baseMessage.setData(vo);
        return baseMessage;
    }

}
